package net.mysoft.budgetsoft.system.action;

import net.mysoft.budgetsoft.system.service.MenuService;
import net.mysoft.framework.base.BaseAction;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

@Results({@Result(name="loadById", location="/platform/menu/update.jsp")})
@Namespace("/menu")
@Scope("prototype")
@Controller
public class MenuAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -211643604541225336L;
	
	@Autowired
	private MenuService menuService;
	
	@Action("tree")
	public String tree() {
		super.writeJson(menuService.getMenuTree());
		return null;
	}
	
	@Action("datagrid")
	public String datagrid() {
		super.writeJson(menuService.getMenuDatagrid(super.getParaMap()));
		return null;
	}
	
	@Action("add")
	public String add() {
		try {
			menuService.addMenu(super.getParaMap());
			super.success("菜单保存成功！！");
		} catch (Exception e) {
			e.printStackTrace();
			super.fail("菜单保存失败！！");
		}
		return null;
	}
	
	@Action("loadMenu")
	public String load() {
		super.setAttr("menu", menuService.getMenu(super.getPara("id")));
		return "loadById";
	}
	
	@Action("update")
	public String update() {
		try {
			menuService.updateMenu(super.getParaMap());
			super.success("菜单保存成功！！");
		} catch (Exception e) {
			e.printStackTrace();
			super.fail("菜单保存失败！！");
		}
		return null;
	}
	
	@Action("del")
	public String del() {
		try {
			menuService.delMenus(super.getPara("ids"));
			super.success("删除成功！！");
		} catch (Exception e) {
			e.printStackTrace();
			super.fail("删除失败！！");
		}
		return null;
	}

}
